---
title: "FE"
author: "Valentina Gonzalez Rostani"
date: "8/5/2023"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}
library(haven)
Panel <- read_dta("C:/Users/vgonz/OneDrive - University of Pittsburgh/Research_Projects/LASA/Datos/all_countries2.dta")

```

# Heterogeneous levels Eco Regulated

```{r}
library(ggplot2)

# Load necessary libraries
# Fit the model
fe <- glm(far_right ~ eco_regulated + factor(pais) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$pais <- factor(Panel$pais)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = eco_regulated, y = yhat, color = pais, shape = pais)) +
  geom_point(size = 4) +
  labs(x = "Economy Desregulated", y = "Far Right - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "pais", labels = levels(Panel$pais)) +
  scale_shape_manual(values = custom_shapes, name = "pais", labels = levels(Panel$pais)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "Far_right_dereg.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "Far_right_dereg.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```


```{r}
# Fit the model
fe <- glm(far_left ~ eco_regulated + factor(pais) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$pais <- factor(Panel$pais)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = eco_regulated, y = yhat, color = pais, shape = pais)) +
  geom_point(size = 4) +
  labs(x = "Economy Desregulated", y = "Far Left - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "pais", labels = levels(Panel$pais)) +
  scale_shape_manual(values = custom_shapes, name = "pais", labels = levels(Panel$pais)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "Far_right_dereg.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "Far_right_dereg.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```
# Heterogeneous effects  Moral



## Abortion

```{r}
library(ggplot2)

# Load necessary libraries
# Fit the model
fe <- glm(far_right ~ val_abortion + factor(pais) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$pais <- factor(Panel$pais)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = val_abortion, y = yhat, color = pais, shape = pais)) +
  geom_point(size = 4) +
  labs(x = "Pro Abortion", y = "Far Right - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "pais", labels = levels(Panel$pais)) +
  scale_shape_manual(values = custom_shapes, name = "pais", labels = levels(Panel$pais)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "eco_moral2Far_left_abortion.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "eco_moral2Far_left_abortion.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```
```{r}
# Fit the model
fe <- glm(far_left ~ val_abortion + factor(pais) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$pais <- factor(Panel$pais)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = val_abortion, y = yhat, color = pais, shape = pais)) +
  geom_point(size = 4) +
  labs(x = "Pro Abortion", y = "Far Left - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "pais", labels = levels(Panel$pais)) +
  scale_shape_manual(values = custom_shapes, name = "pais", labels = levels(Panel$pais)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "eco_moral2Far_left_abortion.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "eco_moral2Far_left_abortion.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```


# Heterogeneous effects Mixed Eco Moral

## Eco_moral

```{r}
library(ggplot2)

# Load necessary libraries
# Fit the model
fe <- glm(far_right ~ Panel$eco_moral2 + factor(pais) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$pais <- factor(Panel$pais)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = eco_moral2, y = yhat, color = pais, shape = pais)) +
  geom_point(size = 4) +
  labs(x = "Mixed Eco-Moral Issues", y = "Far Right - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "pais", labels = levels(Panel$pais)) +
  scale_shape_manual(values = custom_shapes, name = "pais", labels = levels(Panel$pais)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "eco_moral2Far_left_dereg.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "eco_moral2Far_left_dereg.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```
```{r}
# Fit the model
fe <- glm(far_left ~ eco_moral2 + factor(pais) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$pais <- factor(Panel$pais)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = eco_moral2, y = yhat, color = pais, shape = pais)) +
  geom_point(size = 4) +
  labs(x = "Mixed Eco-Moral Issues", y = "Far Left - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "pais", labels = levels(Panel$pais)) +
  scale_shape_manual(values = custom_shapes, name = "pais", labels = levels(Panel$pais)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "eco_moral2Far_left_dereg.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "eco_moral2Far_left_dereg.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```



# Wave

```{r}
library(ggplot2)

# Load necessary libraries
# Fit the model
fe <- glm(far_right ~ eco_moral2 + factor(wave) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$wave <- factor(Panel$wave)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = eco_moral2, y = yhat, color = wave, shape = wave)) +
  geom_point(size = 4) +
  labs(x = "Mixed Eco-Moral Issues", y = "Far Right - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "wave", labels = levels(Panel$wave)) +
  scale_shape_manual(values = custom_shapes, name = "wave", labels = levels(Panel$wave)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "wave_mix.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "wave_mix.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```

```{r}
library(ggplot2)

# Load necessary libraries
# Fit the model
fe <- glm(far_left ~ eco_moral2 + factor(wave) - 1, data = Panel, family = binomial)

# Generate fitted values for all observations in Panel
Panel$yhat <- predict(fe, newdata = Panel, type = "response")

# Check lengths
length(Panel$yhat)  # Should be 5758
nrow(Panel)         # Should also be 5758
# Load the ggplot2 library
library(ggplot2)

# Ensure 'pais' is a factor
Panel$wave <- factor(Panel$wave)

# Define custom shapes and colors
custom_shapes <- c(18, 16, 17, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
custom_colors <- c("#FF0000", "#FF5A00", "#FFB400", "#F0FF00", 
                   "#96FF00", "#3CFF00", "#00FF1E", "#00FF78", 
                   "#00FFD2", "#00D2FF", "#0078FF", "#001EFF", 
                   "#3C00FF", "#9600FF", "#F000FF", "#FF00B4", 
                   "#FF005A")

# Create the plot using ggplot2 with custom shapes and colors
my_plot <- ggplot(Panel, aes(x = eco_moral2, y = yhat, color = wave, shape = wave)) +
  geom_point(size = 4) +
  labs(x = "Mixed Eco-Moral Issues", y = "Far Left - Predicted Values") +
  scale_color_manual(values = custom_colors, name = "wave", labels = levels(Panel$wave)) +
  scale_shape_manual(values = custom_shapes, name = "wave", labels = levels(Panel$wave)) +
  theme_minimal() + 
  theme(axis.line = element_line(color = "black"))

# Save the plot in PDF and JPG formats
ggsave(filename = "wave_mixleft.pdf", plot = my_plot, width = 5, height = 6)
ggsave(filename = "wave_mixleft.jpg", plot = my_plot, width = 5, height = 6)
my_plot
```

# Formal test

```{r}
library(plm)
```
```{r}

lagypool <- plm(far_left ~  eco_moral2   - 1, data=Panel, model="pooling")
lagyfe <- plm(far_left ~   eco_moral2   - 1, data=Panel, model="within")

lagypool2 <- plm(far_right ~  eco_moral2   - 1, data=Panel, model="pooling")
lagyfe2 <- plm(far_right ~   eco_moral2   - 1, data=Panel, model="within")
```

## Lagrange Multiplier Test (Breusch-Pagan) 

I run the Lagrange Multiplier Test (Breusch-Pagan) to analyze if random effects are required. This test fits a linear regression model to the residuals of a linear regression model, and then rejects the null too much of the variance is explained by the additional explanatory variables.

There are significant difference between the estimators. That is, too much of the variance of the residuals is explained by the additional explanatory variables.  RE performs better than pooled OLS.
 
```{r}
plm::plmtest(lagypool, type=c("bp"))

```
```{r}
plm::plmtest(lagypool2, type=c("bp"))

```

## FE vs RE: Hausman Test

```{r}
pFtest(lagyfe, lagypool)

```
```{r}
pFtest(lagyfe2, lagypool)

```


Once, we defined that OLS was worse than RE or FE for this data, we turn our attention to these other model accounting for intercept heterogeneity.

To check the decision whether to use fixed effects or random effect, I run the Hausman Test. Note, that the Hausman tests is interested in the correlation between the unique errors and the regressors in the model. Its null hypothesis is that there is not correlation between the errors and regressors of the models, therefore the models will be consistent.

When the null hypothesis is rejected this means that the errors are correlated to the regressors, thus both models (random effects and fixed effects) will not be consistent.

When we do not reject the null hypothesis, then since both models are consistent, then we should be better using random effects since they are more efficient. However, if we reject the null, then only fixed effects will be admitted. This implies for instance, that the coefficients for OLS and RE may be bias.